package com.ms.oschina.ui.activity;

import java.io.File;
import java.io.FileOutputStream;

import android.content.Intent;
import android.os.Environment;
import android.view.View;

import com.ms.oschina.R;
import com.ms.oschina.api.remote.OsChinaApi;
import com.ms.oschina.base.LoadingPager.ResultCode;
import com.ms.oschina.bean.Blog;
import com.ms.oschina.bean.BlogDetail;
import com.ms.oschina.bean.Constants;
import com.ms.oschina.bean.News;
import com.ms.oschina.bean.News.Relative;
import com.ms.oschina.bean.NewsDetail;
import com.ms.oschina.util.HTMLUtil;
import com.ms.oschina.util.StringUtils;
import com.ms.oschina.util.UiUtils;
import com.ms.oschina.viewholder.NewsDetailTitleViewHoder;

public class NewsDetailsActivity extends WebpageDetailsActivity {

	private News news;

	private NewsDetail newsDetails;
	private BlogDetail blogDetails;
	private int currentMark;
	private Blog blog;
	private NewsDetailTitleViewHoder vh;

	private void setTitle() {
		String author = null;
		String pubDate = null;
		String title = null;
		if (currentMark == Constants.MARK_NEWSINFODETAILS) {// 新闻页面发起点击
			author = news.getAuthor();
			pubDate = news.getPubDate().substring(0, 11);
			title = news.getTitle();
		} else if (currentMark == Constants.MARK_NEWSBLOGDETAILS) {// 博客页面发起点击
			author = blog.getAuthor();
			pubDate = blog.getPubDate().substring(0, 11);
			title = blog.getTitle();
		}
		vh.setData(author, pubDate, title);
	}

	private void setBlogInfoData(StringBuffer body) {
		// 为html添加js代码可以再内容中打开图片查看图片大图
		body.append(HTMLUtil.setHtmlCotentSupportImagePreview(blog.getBody()));
		// 添加代码css样式
		body.append(HTMLUtil.WEB_STYLE).append(HTMLUtil.WEB_LOAD_IMAGES);
	}

	private void setNewsInfoData(StringBuffer body) {
		// 为html添加js代码可以再内容中打开图片查看图片大图
		body.append(HTMLUtil.setHtmlCotentSupportImagePreview(news.getBody()));
		// 添加代码css样式
		body.append(HTMLUtil.WEB_STYLE).append(HTMLUtil.WEB_LOAD_IMAGES);
		// 更多关于***软件的信息
		String softwareName = news.getSoftwareName();
		String softwareLink = news.getSoftwareLink();
		if (!StringUtils.isEmpty(softwareName)
				&& !StringUtils.isEmpty(softwareLink))
			body.append(String
					.format("<div id='oschina_software' style='margin-top:8px;color:#FF0000;font-weight:bold'>更多关于:&nbsp;<a href='%s'>%s</a>&nbsp;的详细信息</div>",
							softwareLink, softwareName));

		// 相关新闻
		if (news != null && news.getRelatives() != null
				&& news.getRelatives().size() > 0) {
			String strRelative = "";
			for (Relative relative : news.getRelatives()) {
				strRelative += String.format(
						"<a href='%s' style='text-decoration:none'>%s</a><p/>",
						relative.url, relative.title);
			}
			body.append("<p/><div style=\"height:1px;width:100%;background:#FFFFFF;margin-bottom:10px;\"/>"
					+ String.format("<br/> <b>相关资讯</b> <div><p/>%s</div>",
							strRelative));
		}
		body.append("<br/>");

		// 保存到本地
		try {
			System.out.println("save begin! :" + news.getId());
			File file = new File(Environment.getExternalStorageDirectory(),
					String.format("/OSChina/html/%d.html", news.getId()));
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			FileOutputStream fos = new FileOutputStream(file);
			fos.write(body.toString().getBytes());
			fos.close();
			System.out.println("save success! :" + file.getPath());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected ResultCode initNetData() {
		try {
			Intent intent = getIntent();
			// 区分是哪个页面请求的数据
			currentMark = intent.getIntExtra(Constants.MARK, 0);
			if (currentMark == Constants.MARK_NEWSINFODETAILS) {// 新闻页面发起点击
				int id = intent.getIntExtra(Constants.NEWSINFODETAILS_ID, -1);
				newsDetails = OsChinaApi.getNewsInfoDetails(id);
				news = newsDetails.getNews();
			} else if (currentMark == Constants.MARK_NEWSBLOGDETAILS) {// 博客页面发起点击
				int id = intent.getIntExtra(Constants.NEWSBLOGDETAILS_ID, -1);
				blogDetails = OsChinaApi.getNewsBlogDetails(id);
				blog = blogDetails.getBlog();
			}
			return ResultCode.STATUS_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultCode.STATUS_ERRORS;
		}
	}

	@Override
	protected CharSequence getBackActionBarTitle() {
		return UiUtils.getString(R.string.actionbar_title_news);
	}

	@Override
	public void onSuccessViewData() {
		setTitle();

		StringBuffer body = new StringBuffer();

		if (currentMark == Constants.MARK_NEWSINFODETAILS) {// 新闻页面发起点击
			setNewsInfoData(body);
			getBottomDetailControlFragment().setCommentCount(news.getCommentCount());
		} else if (currentMark == Constants.MARK_NEWSBLOGDETAILS) {// 博客页面发起点击
			// 设置博客数据
			setBlogInfoData(body);
			getBottomDetailControlFragment().setCommentCount(blog.getCommentCount());
		}

		getWvDetailsContent().loadDataWithBaseURL("", body.toString(),
				"text/html", "utf-8", "");
	}

	@Override
	public View getTitleView() {
		vh = new NewsDetailTitleViewHoder();
		return vh.getRootView();
	}

}
